<template>
  <div id="app">
      <keep-alive>
          <router-view/>
      </keep-alive>

  </div>
</template>

<script>
export default {
  name: 'App',
    data () {
        return {
            transitionName: 'slide-left'
        }
    },
    created () {
        if(window.localStorage.getItem('access_token')!='null'){
            this.userInfo();
        }

    },
    methods:{
        userInfo(){
            var _this=this;
            this.$axios.get(this.$axios.defaults.baseURL+'userInfo',{

            }).then(function (res) {
                if(res.data.state&&res.data.data!=null){
                    _this.$store.commit('userInfo',{
                        user_id:res.data.data.id,
                        thumb:res.data.data.thumb,
                        name:res.data.data.name,
                        mobile:res.data.data.mobile,
                        article_count:res.data.data.article_count,
                        like_user_count:res.data.data.like_user_count,
                        fans_count:res.data.data.fans_count
                    })
                }else{
                    window.localStorage.setItem('access_token',null)
                }
            }).catch(function (err) {
                console.log(err)
            })
        },
        handle(e) {
            function addHandler(el, type, handler) {
                el.addEventListener(type, handler, false)
            }

            addHandler(e.target, 'plusready', (e) => {
                e.push();

            })
        },
        push(){

            var Push = function() {

            }
            /**
             *
             * 推送的监听
             */
            Push.prototype.pushListener = function () {
                var _this = this;
                plus.push.addEventListener("click", function (msg) {
                    switch (msg.payload) {
                        case "LocalMSG":
                            ApiConfig.staticToast("点击本地创建消息启动：");
                            break;
                        default:
                            ApiConfig.staticToast("点击离线推送消息启动：");
                            break;
                    }
                    if (msg.payload) {
                        _this.handle(msg);
                    }
                }, false);
                plus.push.addEventListener("receive", function (msg) {
                    if (msg.aps) { // Apple APNS message
                        ApiConfig.staticToast("接收到在线APNS消息：");
                    } else {
                        ApiConfig.staticToast("接收到在线透传消息：");
                    }
                    if (plus.os.name == 'iOS') {
                        if (msg.payload) {
                            _this.notificationMessage(msg);
                        }
                    } else {
                        _this.notificationMessage(msg);
                    }
                }, false);
            }

            /**
             * 根据推送消息在通知栏中显现对应的提示
             * @param {Object} msg
             */
            Push.prototype.notificationMessage = function(msg) {
                ApiConfig.staticToast(msg, 1);
                ApiConfig.staticIsDebug("notificationMessage", msg, 1);
                var content = '';//你要展示的提示
                var _this = this;
                var jsonData = '';
                switch(plus.os.name) {
                    case "Android":
                        jsonData = eval("(" + msg.payload + ")");
                        break;
                    case "iOS":
                        jsonData = msg.payload;
                        break;
                }
                _this.createLocalPushMsg(msg, content);
            }

            /**
             *  处理透传消息
             * @param {Object} msg
             * @param {Object} content
             */
            Push.prototype.createLocalPushMsg = function(msg, content) {
                //创建一个符合你自己要显示推送通知
                _this.createMessage(str, jsonData, options);
            }

            /**
             *  创建本地推送消息
             * @param {Object} str
             * @param {Object} jsonData
             * @param {Object} options
             */
            Push.prototype.createMessage = function(str, jsonData, options) {
                switch(plus.os.name) {
                    case "Android":
                        jsonData = jsonData;
                        break;
                    case "iOS":
                        jsonData = jsonData.eid;
                        break;
                }
                plus.push.createMessage(str, jsonData, options);
            }

            /**
             * 处理通知方法
             * @param {Object} msg
             */
            Push.prototype.handle = function(msg) {
                //清除ios推送小红点
                this.cancelPushclear();
            }
            Push.prototype.cancelPushClear = function () {
                plus.push.clear();
            }



        }

    },
    watch: {
      $route() {
          if(window.localStorage.getItem('access_token')!='null'){
              this.userInfo();
          }
      }
    }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}
</style>
<style lang="less">
  @import '~vux/src/styles/1px.less';
  @import '~vux/src/styles/close.less';
</style>
